<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Config_utility
 *
 * @author peerawatj20
 */
class Config_utility {

    var $DAY = 'day';
    var $MONTH = 'month';
    var $YEAR = 'year';

    public function list_day() {
        $arrDay = array();
        $arrDay[''] = 'วัน';
        for ($i = 1; $i <= 31; $i++) {
            $arrDay[$i] = $i;
        }
        return $arrDay;
    }

    public function list_month() {
        $arrayMonth = array(
            '' => 'เดือน',
            '1' => 'มกราคม',
            '2' => 'กุมภาพันธ์',
            '3' => 'มีนาคม',
            '4' => 'เมษายน',
            '5' => 'พฤษภาคม',
            '6' => 'มิถุนายน',
            '7' => 'กรกฎาคม',
            '8' => 'สิงหาคม',
            '9' => 'กันยายน',
            '10' => 'ตุลาคม',
            '11' => 'พฤศจิกายน',
            '12' => 'ธันวาคม'
        );
        return $arrayMonth;
    }

    public function list_year() {
        $arrYear = array();
        $num_year = 0;
        $year = date("Y") + 543;
        $arrYear[''] = 'ปี';
        for ($i = 0; $i < 20; $i++) {
            if ($i == 0) {
                $num_year = $year;
                $arrYear[$year] = $year;
            } else {
                $num_year = $num_year - 1;
                $arrYear[$num_year] = $num_year;
            }
        }
        return $arrYear;
    }

    public function convert_date($date, $type) {
        switch ($type) {
            case $this->DAY: return substr($date, 8, 2);
            case $this->MONTH: return substr($date, 5, 2);
            case $this->YEAR: return substr($date, 0, 4);
        }
    }

    public function month_to_string($month) {
        $arrayMonth = array(
            '' => 'เดือน',
            '01' => 'มกราคม',
            '02' => 'กุมภาพันธ์',
            '03' => 'มีนาคม',
            '04' => 'เมษายน',
            '05' => 'พฤษภาคม',
            '06' => 'มิถุนายน',
            '07' => 'กรกฎาคม',
            '08' => 'สิงหาคม',
            '09' => 'กันยายน',
            '10' => 'ตุลาคม',
            '11' => 'พฤศจิกายน',
            '12' => 'ธันวาคม'
        );

        return $arrayMonth[$month];
    }

    public function format_telephone($telephone) {
        $format = '';
        if (strlen($telephone) == 9) {
            $str1 = substr($telephone, 0, 3);
            $str2 = substr($telephone, 3);
            $format = $str1 . '-' . $str2;
        } else if (strlen($telephone) == 10) {
            $str1 = substr($telephone, 0, 2);
            $str2 = substr($telephone, 2, 4);
            $str3 = substr($telephone, 6, 4);
            $format = $str1 . ' ' . $str2 . ' ' . $str3;
        }
        return $format;
    }

    public function format_citizenId($citizenId) {
        $format = '';
        $str1 = substr($citizenId, 0, 1);
        $str2 = substr($citizenId, 1, 4);
        $str3 = substr($citizenId, 5, 5);
        $str4 = substr($citizenId, 10, 2);
        $str5 = substr($citizenId, 12, 1);
        $format = $str1 . ' ' . $str2 . ' ' . $str3 . ' ' . $str4 . ' ' . $str5;
        return $format;
    }

    public function check_str($str) {
        $str = trim($str);
        if (!isset($str) || is_null($str) || empty($str)) {
            $str = '-';
        }
        return $str;
    }

    public function format_number($str) {
        $length = strlen($str);
        $result = '';
        $arrStr = array();

        for ($i = 0; $i < $length; $i++) {
            $arrStr[$length - ($i + 1)] = substr($str, $i, 1);
        }

        for ($i = 0; $i < $length; $i++) {
            $result = $arrStr[$i] . $result;
            if (($i + 1) % 3 == 0 && $i != 0 && $i != ($length - 1)) {
                if ($arrStr[$i] != '.') {
                    $result = ',' . $result;
                }
            }
        }

        return $result;
    }

}
